<template>
  <div class="app-container">
    <el-form :inline="true" :model="formInlinetwo" class="demo-form-inline" label-width="80px">
      
      <el-form-item label="加载活动">
        <el-input v-model="formInlinetwo.code" placeholder="输入Code"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="reload">加载活动</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="handleCupdate(-1,0,0)">创建活动</el-button>
      </el-form-item>
    </el-form>
    <el-dialog :title="set==0?'创建活动':'编辑活动'" :visible.sync="dialogFormVisiblea" width="25%">
     <el-form ref="form" :model="form" label-position="left" label-width="100px">
           <el-form-item label="预定人数" :label-width="formLabelWidth">
             <el-input v-model="form.reserve" autocomplete="off" placeholder="输入预定人数" onkeyup="value=value.replace(/[^\d]/g,'')"></el-input>
           </el-form-item>
         <el-form-item label="日期" :label-width="formLabelWidth">
          <el-date-picker
                v-model="form.day"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
         </el-form-item>
         <el-form-item label="Code" :label-width="formLabelWidth">
           <el-input v-model="form.code" autocomplete="off" placeholder="输入Code" onkeyup="value=value.replace(/[^\d]/g,'')"></el-input>
         </el-form-item>
         <el-form-item label="设置" :label-width="formLabelWidth">
           <el-input  v-model="form.setting" autocomplete="off" placeholder="输入题库号"></el-input>
         </el-form-item>
         <el-form-item label="主题" :label-width="formLabelWidth">
           <el-input v-model="form.theme" autocomplete="off" placeholder="输入主题"></el-input>
         </el-form-item>
         <el-form-item label="海报" :label-width="formLabelWidth">
           <el-input v-model="form.note" autocomplete="off" placeholder="输入海报"></el-input>
         </el-form-item>
       <el-form-item>
         <el-button type="primary" size="mini" @click="onSubmits">确定</el-button>
         <el-button size="mini" @click="Cancel">取消</el-button>
       </el-form-item>
     </el-form>
    </el-dialog>

    <el-table
       :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
       style="width: 100%">
       <el-table-column
         label="actID"
         width="100">
         <template slot-scope="scope">
           <span>{{ scope.row.actID }}</span>
         </template>
       </el-table-column>
       <el-table-column
         label="orgID"
         width="100">
         <template slot-scope="scope">
           <span>{{ scope.row.orgID }}</span>
         </template>
       </el-table-column>
       <el-table-column
         label="真实人数"
         width="100">
         <template slot-scope="scope">
           <span>{{ scope.row.real }}</span>
         </template>
       </el-table-column>
       <el-table-column
         label="预定人数"
         width="100">
         <template slot-scope="scope">
           <span>{{ scope.row.reserve }}</span>
         </template>
       </el-table-column>
       <el-table-column
         label="状态"
         width="100">
         <template slot-scope="scope">
           <span>{{ scope.row.status }}</span>
         </template>
       </el-table-column>
       <el-table-column
         label="日期"
         width="200">
         <template slot-scope="scope">
           <span>{{ scope.row.day }}</span>
         </template>
       </el-table-column>
       <el-table-column
         label="Code"
         width="100">
         <template slot-scope="scope">
           <span>{{ scope.row.code }}</span>
         </template>
       </el-table-column>
       <el-table-column
         label="设置"
         width="150">
         <template slot-scope="scope">
           <span v-for="(item,index) in scope.row.setting" :key="index">{{item}}</span>
         </template>
       </el-table-column>
       <el-table-column
         label="主题"
         width="100">
         <template slot-scope="scope">
           <span>{{ scope.row.theme }}</span>
         </template>
       </el-table-column>
       <el-table-column
         label="备注"
         width="100">
         <template slot-scope="scope">
           <span>{{ scope.row.note }}</span>
         </template>
       </el-table-column>
       <el-table-column label="操作">
         <template slot-scope="scope">
           <el-button
             size="mini"
             type="primary"
             @click="handleCupdate(scope.$index,1,scope.row.actID)">编辑</el-button>
             <el-button
             size="mini"
             type="primary"
             @click="handleremove(scope.row.code)">移除</el-button>
               <el-button
             size="mini"
             type="primary"
             @click="handlereset(scope.row.code)">重置</el-button>
              <el-button
             size="mini"
             type="primary"
             @click="handleinact(scope.row.code)">处理活动</el-button>
            
         </template>
       </el-table-column>
     </el-table>
     <div class="pages" v-if="tableData!=''">
       <div class="block">
         <el-pagination
           @current-change="handleCurrentChange"
           :current-page.sync="currentPage"
           :page-size="pagesize"
           layout="prev, pager, next, jumper"
           :total="total">
         </el-pagination>
       </div>
      </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Method from '@/utils/proto'
import {posts} from '@/api/api'
import Error from '@/utils/error'
import http from '@/api/api'

export default {
    data() {
      return {
        tableData: [],
        formLabelWidth: '120px',
        dialogFormVisiblea:false,
        dialogFormVisibleb:false,
        urls:'',
        formInline: {
           beginDay: '',
           endDay:''
        },
        set:0,
        actid:0,
        formInlinetwo:{
          code:''
        },
        findform:{
          code:''
        },
        form: {
          method:Method.Method.MSG_AGENT_CREATE_ACT,
          token:this.$store.getters.token,
          reserve:'',
          status:0,
          day:'',
          type:'',
          code:'',
          setting:'',
          theme: 'comm',
          note:' '
        },
        currentPage: 1,
        total:100,
        pagesize:15
      }

    },
    methods: {
      handleinact(code){
        let roles = this.$store.getters.roles
      if(roles!=111111111&&roles==11111110&&roles==11111100&&roles==11111000){
         this.$message({
           type:'warning',
           message:'没有权限执行此操作'
         })
         return
       }
        let data={
          method:Method.Method.MSG_AGENT_DEAL_ACT,
          token:this.$store.getters.token,
          code:code 
        }
        posts(data).then((res) => {
           if (res.result == 0) {
                this.$message({
                  type: "success",
                  message: "处理成功!",
                });
                this.$store.commit('SET_CODE',code)
            }else{
            this.$message({
                  type: 'error',
                  message: Error.getErrorMessage(res.result),
                });
         }
        }).catch()
      },
      handlereset(code){
        let roles = this.$store.getters.roles
       if(roles==10000000||roles==11000000||roles=='00000000'){
         this.$message({
           type:'warning',
           message:'没有权限执行此操作'
         })
         return
       }
        let data = {
           method:Method.Method.MSG_AGENT_RESET_ACT,
          token:this.$store.getters.token,
          code:code
        }
        posts(data).then((res) => {
         if (res.result == 0) {
                this.$message({
                  type: "success",
                  message: "重置成功!",
                });
           this.onloadList()
         }else{
            this.$message({
                  type: 'error',
                  message: Error.getErrorMessage(res.result),
                });
         }
      }).catch()
      },
      handleCurrentChange(val){
      this.currentPage = val;
      },
      handleremove(e){
        if(roles==10000000||roles==11000000||roles==11100000||roles=='00000000'){
              this.$message({
                type:'warning',
                message:'没有权限执行此操作'
              })
              return
            }
        let data = {
          method:Method.Method.MSG_AGENT_REMOVE_ACT,
          token:this.$store.getters.token,
          code:e
        }
        this.$confirm("此操作将移除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
          posts(data)
            .then((res) => {
              if (res.result == 0) {
                this.$message({
                  type: "success",
                  message: "移除成功!",
                });
                this.onloadList()
              } else {
                this.$message({
                  type: "info",
                  message: "移除失败",
                });
              }
            })
            .catch();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
   
      },
      handlerestore(){
       let data={
         method:Method.Method.MSG_AGENT_RESTORE_ACT,
         token:this.$store.getters.token,
         filename:''
       }
       posts(data).then((res) => {
      }).catch()
      },
      myallactive(){},
      allactive(){},
      activelist(){},
      Cancel(){
       this.dialogFormVisiblea=false
      },
      onSubmit(){
        let data={
          method:Method.Method.MSG_AGENT_LIST_ACT,
          token:this.$store.getters.token,
          type:'',
          beginDay:this.formInline.beginDay,
          endDay:this.formInline.endDay
        }
        posts(data).then((res) => {
      }).catch()
      },
      handleCupdate(index,set,actid){
      let roles = this.$store.getters.roles
      if(set==0){
      if(roles==10000000||roles==11000000||roles==11100000||roles=='00000000'){
              this.$message({
                type:'warning',
                message:'没有权限执行此操作'
              })
              return
            }
      }else{
        if(roles==10000000||roles==11000000){
         this.$message({
           type:'warning',
           message:'没有权限执行此操作'
         })
         return
       }  
      }
    console.log(66);
        this.set = set      
        if(set==1){
          this.form.reserve = this.tableData[index].reserve;
          this.form.note = this.tableData[index].note;
          this.form.theme = this.tableData[index].theme;
          this.form.setting = this.tableData[index].setting.libraryID;
          this.form.code = this.tableData[index].code;
          this.form.day = this.tableData[index].day;
          this.form.status = this.tableData[index].status;
          this.actid = actid   
        }else{
          this.form.reserve = ''
          this.form.note = ''
          this.form.theme = 'comm'
          this.form.setting = ''
          this.form.code = ''
          this.form.day = ''
          this.form.status = 0
        }     
       this.dialogFormVisiblea=true 
      },
      onSubmits(){
        const that = this
        let data = that.form
        let dayStr = data.day
        let setting = data.setting
        let newset = `{"library":${setting}}`
        let set = that.set
        const date = new Date(dayStr);
        const day = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
        that.form.setting = newset
        that.form.day = day
        switch (set) {
          case 0:
            that.form.method = Method.Method.MSG_AGENT_CREATE_ACT
            // this.urls = 'http://cp.coolplayable.com:8080/xuekai/Editor0.2.1/?'+http+';'+data.code+';'+data.theme
            this.$router.push({name:'map',params: { code: data.code,theme:data.theme }})
            break;         
          case 1:
            that.form.method = Method.Method.MSG_AGENT_UPDATE_ACT
            that.form.actID = that.actid
            break;
        }
        posts(that.form).then((res) => {
        this.dialogFormVisiblea=false
        if(res.result==0){
            this.onloadList()
            this.$message({
              type: "success",
              message: set==0?'创建成功！':'编辑成功！',
            });}
      }).catch()
      },
      reload(){
        let roles = this.$store.getters.roles
        if(roles==10000000||roles==11000000||roles==11100000){
         this.$message({
           type:'warning',
           message:'没有权限执行此操作'
         })
         return
       }
        
        let data={
          method:Method.Method.MSG_AGENT_RELOAD_ACT,
          token:this.$store.getters.token,
          code:parseInt(this.formInlinetwo.code)
        }
        console.log(data);
        posts(data).then((res) => {
          console.log(res);
          if(res.result==0||res.result==9){
            this.onloadList()
            this.$message({
              type: "success",
              message: "加载成功!",
            });
          }else{
            this.$message({
                  type: 'error',
                  message: Error.getErrorMessage(res.result),
                });
         }
      }).catch()
      },
      onSubFind(){
        const that = this
         let data = {
          method:Method.Method.MSG_AGENT_FIND_ACT,
          token:this.$store.getters.token,
          code:that.findform.code
        }
        posts(data).then((res) => {
         this.tableData = res.list
      }).catch()
      },
      onloadList(){
        let data={
        method:Method.Method.MSG_AGENT_REAL_ACT_LIST,
        token:this.$store.getters.token,
      }
       posts(data).then((res) => {
         this.tableData = res.list
         this.total = res.list.length
      }).catch()
      }
      
    },
    created: function(){
      this.onloadList()

     },
  }
</script>

<style lang="scss" scoped>
 .pages{
    width: 100%;
    margin-top: 2%;
    display: flex;
    justify-content: center;
  }
  .line{
    text-align: center;
  }
</style>
